<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_order_product;

use Plugin\Attractions\Commerce\Model\Commerce_order_product\CommerceOrderProduct;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 订单商品 Repository类
 */
class CommerceOrderProductRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceOrderProduct $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 订单号
        if (isset($params['order_id']) && !empty($params['order_id'])) {
            $query->where('order_id', '=', $params['order_id']);
        }

        // 商品ID
        if (isset($params['commodity_id']) && !empty($params['commodity_id'])) {
            $query->where('commodity_id', '=', $params['commodity_id']);
        }

        // 商品名称
        if (isset($params['commodity_name']) && !empty($params['commodity_name'])) {
            $query->where('commodity_name', 'like', '%'.$params['commodity_name'].'%');
        }

        // 数量
        if (isset($params['quantity']) && !empty($params['quantity'])) {
            $query->where('quantity', '=', $params['quantity']);
        }

        // 单价
        if (isset($params['price']) && !empty($params['price'])) {
            $query->where('price', '=', $params['price']);
        }

        // 折扣
        if (isset($params['discount']) && !empty($params['discount'])) {
            $query->where('discount', '=', $params['discount']);
        }

        // 总价
        if (isset($params['total_price']) && !empty($params['total_price'])) {
            $query->where('total_price', '=', $params['total_price']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}